/*
 * Copyright 2007 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package be.jaisy.project.ui.client;


import be.jaisy.project.service.api.IProjectServiceAsync;
import be.jaisy.project.service.api.to.ProjectTo;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 */
public class Project implements EntryPoint {

	private VerticalPanel panel = new VerticalPanel();
	private IProjectServiceAsync simpleService = (IProjectServiceAsync) GWT.create(be.jaisy.project.service.api.IProjectService.class);
	private Button button = new Button("call osgi service...");
	private Label label = new Label("text");
	private TextArea textArea = new TextArea();

	public void onModuleLoad() {
				
		panel.setWidth("100%");
		panel.setHeight("100%");
		textArea.setHeight("200");
		textArea.setWidth("90%");
		//button.setText("click here");

		ServiceDefTarget endpoint = (ServiceDefTarget) simpleService;
		String moduleRelativeURL = "http://localhost:8080/osgiRemoteCall";
		endpoint.setServiceEntryPoint(moduleRelativeURL);

		button.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				label.setText("initiate call to "+((ServiceDefTarget)simpleService).getServiceEntryPoint());
				AsyncCallback callback = new AsyncCallback() {
					public void onSuccess(Object result) {
						textArea.setText((String)result);
					}

					public void onFailure(Throwable caught) {
						textArea.setText((String)caught.getMessage());
					}
				};
				
				ProjectTo projectTo = new ProjectTo();
				projectTo.setName("project1");
				projectTo.setName("project1");
				
				simpleService.createProject(projectTo, callback);
			}
		});
		panel.add(button);
		panel.add(label);
		panel.add(textArea);		
		RootPanel.get().add(panel);
	}
}
